﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using starLiGHT.Collections;
using Microsoft.Xna.Framework;

namespace starLiGHT.Engine.Tests
{
    [TestFixture]
    public class WeightedList
    {
        WeightedList<int> wList;

        [SetUp]
        public void Init()
        {
            wList = new WeightedList<int>();
        }

        [Test]
        public void WeightValuePair01()
        {
            WeightValuePair<int> wvp = new WeightValuePair<int>(1.0f, 2);

            Assert.AreEqual(1.0f, wvp.Weight);
            Assert.AreEqual(2, wvp.Value);
        }

        [Test]
        public void WeightValuePair02()
        {
            WeightValuePair<int> wvp = new WeightValuePair<int>(1.0f, 2);

            Assert.AreEqual("[1, 2]", wvp.ToString());
        }

        [Test]
        public void WeightedList01()
        {
            wList.Add(1);

            Assert.AreEqual(1, wList[0]);

            wList[0] = 2;

            Assert.AreEqual(2, wList[0]);
        }

        [Test]
        public void WeightedList02()
        {
            wList.Add(1);
            wList.Add(2);

            Assert.AreEqual(1, wList[0]);
            Assert.AreEqual(2, wList[1]);

            wList.Clear();

            Assert.Catch<IndexOutOfRangeException>(delegate { int x = wList[1]; });
        }

        [Test]
        public void WeightedList03()
        {
            wList.Add(50.0f, 1);
            wList.Add(50.0f, 2);

            for (int i = 0; i < 1000000; i++)
            {
                int test = wList.RandomValue;
                Assert.GreaterOrEqual(test, 1);
                Assert.LessOrEqual(test, 2);
            }
        }

        [Test]
        public void WeightedList04()
        {
            wList.Add(100.0f, 1);
            wList.Add(0.0f, 2);

            for (int i = 0; i < 1000000; i++)
            {
                int test = wList.RandomValue;
                Assert.AreEqual(1, test);
            }
        }

        [Test]
        public void WeightedList05()
        {
            wList.Add(90.0f, 1);
            wList.Add(10.0f, 2);
            wList.Add(0.0f, 3);

            int oneCount = 0, twoCount = 0;

            for (int i = 0; i < 1000000; i++)
            {
                int test = wList.RandomValue;

                if (test == 1) oneCount++;
                if (test == 2) twoCount++;
                if (test == 3) Assert.Fail();
            }

            Assert.Greater(oneCount, twoCount);
        }

        [Test]
        public void WeightedList06()
        {
            wList.Add(90.0f, 1);
            wList.Add(10.0f, 2);
            wList.Add(11.11f, 3);

            Assert.AreEqual(111.11f, wList.TotalWeight);
        }

    }
}
